/**
* 新增试卷
**/
<template>
  <div>
    <el-container>
      <el-header>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>试卷管理</el-breadcrumb-item>
          <el-breadcrumb-item>添加试卷</el-breadcrumb-item>
        </el-breadcrumb>
      </el-header>
      <el-steps :active="active" style="width: 70%; margin: auto;
  position: relative;" finish-status="success">
        <el-step title="试卷基本信息" description="进行中"></el-step>
        <el-step title="添加试题" description="未完成"></el-step>
        <el-step title="试卷设置" description="未完成"></el-step>
      </el-steps>
      <p />
      <p />
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label=" 试卷名称：" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="试卷难度：" prop="diffculty">
          <el-radio-group v-model="ruleForm.diffculty">
            <el-radio label="easy">容易</el-radio>
            <el-radio label="normal">一般</el-radio>
            <el-radio label="diffculty">困难</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="试卷类型：" prop="type">
          <el-radio-group v-model="ruleForm.type">
            <el-radio label="exact">固定试题</el-radio>
            <el-radio label="random">随机试题</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="试卷分类：">
          <div class="block">
            <el-cascader :options="tableData" :props="cateListProps" @change="handlechange">
              <template slot-scope="{ node }">
                <span>{{ node.label }}</span>
                <span v-if="!node.isLeaf"> ({{ node.children.length }}) </span>
              </template>
            </el-cascader>
          </div>
        </el-form-item>
        <el-form-item label="试卷状态：" prop="Pstatus">
          <el-switch v-model="ruleForm2.Pstatus" active-text="启用" active-color="#13ce66" inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleForm)">提交试卷</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>

    </el-container>
  </div>
</template>

<script>
//导入组件
import { addPaper, pList } from '../../../api/paperMG'
//导出模块
export default {
  data() {
    return {
      tableData: [],
      //步骤
      active: 0,
      //表单元素
      ruleForm: {
        name: '',
        diffculty: '',
        type: '',
        status: '',
        cid: ''
      },
      ruleForm2: {
        Pstatus: true
      },
      // rules表单验证
      rules: {
        name: [
          { required: true, message: '请输入试卷名称', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
      },
      cateListProps: {
        value: 'id',
        label: 'name',
        children: 'paperCategories',
        checkStrictly: true,
      },
      formInline: {
        page: 1,
        limit: 10,
        categoryName: '',
        grade: '',
      },
    };
  },

  methods: {
    submitForm(parameter) {
      if (parameter.name == '') {
        this.$message({
          message: '请填写试卷名称',
          type: 'warning',
        })
      } else {
        if (this.ruleForm2.Pstatus) {
          parameter.status = 'y'
        } else {
          parameter.status = 'n'
        }
        addPaper(parameter).then((res) => {
          if (res.data.code == 200) {
            this.$message({
              type: 'success',
              message: "添加成功",
            })
            this.$router.replace("/system/questionList")
          }
        })
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //给试卷添加试卷分类
    handlechange(value) {
      this.ruleForm.cid = value[value.length - 1]
    },
    getdata() {
      /***
       * 调用接口，渲染试卷分类列表
       */
      pList().then((res) => {
        if (res.data.code == 200) {
          this.tableData = this.formatData(res.data.data)
        }
      })
        .catch((err) => {
          this.$message.error('获取试卷列表失败，请稍后再试！')
        })
    },
    // 格式化数据，递归将空的children置为undefined
    formatData(data) {
      for (var i = 0; i < data.length; i++) {
        if (data[i].paperCategories.length < 1) {
          data[i].paperCategories = undefined
        } else {
          this.formatData(data[i].paperCategories)
        }
      }
      return data
    },
  },

  // 创建完毕状态(里面是操作)
  created() {
    this.getdata(this.formInline)
  },
}

</script>

<style>
.demo-ruleForm {
  width: 700px;
  margin: auto;
  position: relative;
}
</style> */